import json
import re
import hashlib
from django.contrib.auth.hashers import pbkdf2
from django.contrib.auth.hashers import make_password, check_password
from datetime import datetime, timedelta
# from apps.utils.generate_account_number import GenerateAccountNumber
from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from django.contrib.auth import get_user_model
# from rest_framework_jwt.utils import jwt_encode_handler, jwt_payload_handler
from rest_framework_jwt.serializers import jwt_encode_handler, jwt_payload_handler
# from rest_framework_jwt.views import obtain_jwt_token
from django.db.models import Q
from django.db import transaction

from common.com_utils import Utils
from common.com_time import Time

from apps.resources.models import SubjectInfoModel, LabelInfoModel, QuestionTypeInfoModel, \
    QuestionLevelInfoModel, QuestionInfoModel


class SubjectInfoSerializers(serializers.ModelSerializer):
    """
    学科信息序列化类
    """

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        return attrs

    class Meta:
        model = SubjectInfoModel
        fields = "__all__"


class LabelInfoSerializers(serializers.ModelSerializer):
    """
    标签信息序列化类
    """

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        if isinstance(attrs['subject'], str):
            attrs['subject'] = int(attrs['subject'])
        return attrs

    class Meta:
        model = LabelInfoModel
        fields = "__all__"


class QuestionTypeInfoSerializers(serializers.ModelSerializer):
    """
    问题类型信息序列化类
    """

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        return attrs

    class Meta:
        model = QuestionTypeInfoModel
        fields = "__all__"


class QuestionLevelInfoSerializers(serializers.ModelSerializer):
    """
    问题难度信息序列化类
    """

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        return attrs

    class Meta:
        model = QuestionLevelInfoModel
        fields = "__all__"


class QuestionInfoSerializers(serializers.ModelSerializer):
    """
    问题信息序列化类
    """

    @staticmethod
    def handle_field_data(web_field_data: str, split_symbol_list: list = ['；', ';']):
        """
        :param web_field_data:前端传来的字段数据
        :param split_symbol_list: 分割符号列表,默认传入中英文输入下的分号
        :return:
        """
        for split_symbol in split_symbol_list:
            options_list = web_field_data.split(split_symbol)
            if options_list.__len__():
                return [text for text in options_list if len(text) != 0]
        return []

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        try:
            if isinstance(attrs['subject'], str):
                attrs['subject'] = int(attrs['subject'])
            if isinstance(attrs['label'], str):
                attrs['label'] = int(attrs['label'])
            if isinstance(attrs['type'], str):
                attrs['type'] = int(attrs['type'])
            if isinstance(attrs['level'], str):
                attrs['level'] = int(attrs['level'])
        except ValueError:
            raise ValueError('输入的必须是整数数字')
        # 处理问题选项数据
        if attrs.get('options', None) is not None:
            text_list = self.handle_field_data(web_field_data=attrs.get('options'))
            if text_list.__len__():
                attrs['options'] = text_list
        # 处理问题答案数据
        if attrs.get('answer', None) is not None:
            text_list = self.handle_field_data(web_field_data=attrs.get('answer'))
            if text_list.__len__():
                attrs['answer'] = text_list
        return attrs

    class Meta:
        model = QuestionInfoModel
        fields = "__all__"
